// pages/problemItemInfo/peoblemItemInfo.js
const app = getApp()
const util = require('../../utils/util.js')
const lbbService = require('../service/service.js')

Page({

  data: {
    handExtra:'',
    areaShow:true,
    pileInfoLog:[],
    typeIndex:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let that = this
    that.setData({
      id: options.id,
      status: options.status,
      scrollHeight: app.getScrollHeight(45)
    })
    if(options.typeId){
      that.setData({
        typeId: options.typeId,
        questionName: options.questionName
      })
    }
    if (that.data.status == "0") {
      lbbService.getPendingProblemDetail({ feedbackId: that.data.id },function(res){
        that.setData({
          problemDetail: res.data
        })
        if (that.data.problemDetail.pileCode != 0) {
          that.getLog()
        }
      })
    } else {
      lbbService.getProcessedProblemDetail({ feedbackId: that.data.id }, function (res) {
        that.setData({
          problemDetail: res.data
        })
        if (that.data.problemDetail.pileCode != 0) {
          that.getLog()
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.personalModal = this.selectComponent("#personalModal")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    if (that.data.typeId){
      lbbService.getSecondTypes({ questionTypeId: parseInt(that.data.typeId) }, function (res) {
        that.setData({
          secondTypeList: res.data
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /** 
   * 预览图片
   */
  previewImage: function (e) {
    var urls = e.target.dataset.src;
    wx.previewImage({
      urls: urls // 需要预览的图片http链接列表
    })
  },
  showTypeModal:function(){
    this.setData({
      isTypeShow:true
    })
  },
  hideTypeModal: function () {
    this.setData({
      isTypeShow: false
    })
  },
  showArea:function(){
    this.setData({
      areaShow:true
    })
  },
  showModal: function(){
    let that = this
    that.setData({
      areaShow: false
    })
    that.personalModal.showModal('是否确定已处理？', that.handIt)
  },
  chooseType:function(e){
    console.log(e)
    this.setData({
      typeIndex: e.currentTarget.dataset.index
    })
  },
  setExtra: function(e){
    let that = this
    that.setData({
      handExtra: e.detail.value
    })
  },
  getLog:function(){
    let that = this
    lbbService.getPileLog({ pileCode: that.data.problemDetail.pileCode },function(res){
      that.setData({
        pileInfoLog: res.data
      })
    })
  },
  handIt: function (){
    let that = this
    that.showArea()
    if (that.data.typeId == 5){
      lbbService.handleProblem({
          feedbackId: that.data.id,
          handExtra: that.data.handExtra,
          secondType: parseInt(that.data.secondTypeList[that.data.typeIndex].questionId)
        },function(res){
          wx.showToast({
            title: '已处理',
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        })
    }else {
      lbbService.handleProblem({
        feedbackId: that.data.id,
        handExtra: that.data.handExtra
      }, function (res) {
        wx.showToast({
          title: '已处理',
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 1500)
      })
    }
  }
})